package com.pu.controller.admin;

import com.pu.dto.DishDTO;
import com.pu.dto.DishPageQueryDTO;
import com.pu.entity.Dish;
import com.pu.result.PageResult;
import com.pu.result.Result;
import com.pu.service.DishService;
import com.pu.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

/**
 * @Author Yusc
 * @Date 2023/10/26 23:21
 * @Version
 * @Description 菜品管理
 * 优化：当菜品发生变动，则清理缓存数据，防止用户端和管理端的数据不一致
 **/
@RestController
@RequestMapping("/admin/dish")
@Api(tags = "菜品相关接口")
@Slf4j
public class DishController {
    @Autowired
    private DishService dishService;
    @Autowired
    private RedisTemplate redisTemplate;

    /**
     * @param dishDTO
     * @auther: Yusc
     * @date 2023/10/26 23:38
     * @Description 新增菜品
     */
    @PostMapping
    @ApiOperation("新增菜品")
    public Result save(@RequestBody DishDTO dishDTO) {
        log.info("新增菜品：{}", dishDTO);
        dishService.saveWithFlavor(dishDTO);

        // 清理缓存数据
        String key = "dish_" + dishDTO.getCategoryId();
        cleanCache(key);
        return Result.success();
    }

    /**
     * @param dishPageQueryDTO
     * @auther: Yusc
     * @date 2023/10/27 0:48
     * @Description 菜品分页查询
     */
    @GetMapping("/page")
    @ApiOperation("菜品分页查询")
    public Result<PageResult> page(DishPageQueryDTO dishPageQueryDTO) {
        log.info("菜品分页查询:{}", dishPageQueryDTO);
        PageResult pageResult = dishService.pageQuery(dishPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * @param ids
     * @return
     * @auther: Yusc
     * @date 2023/10/27 11:07
     * @Description 菜品批量删除
     */
    @DeleteMapping
    @ApiOperation("菜品批量删除")
    public Result delete(@RequestParam List<Long> ids) {
        log.info("菜品批量删除：{}", ids);
        dishService.deleteBatch(ids);

        // 将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * @param status
     * @param id
     * @auther: Yusc
     * @date 2023/10/28 17:58
     * @Description 菜品起售和停售
     */
    @PostMapping("/status/{status}")
    @ApiOperation("菜品起售和停售")
    public Result<String> startOrStop(@PathVariable("status") Integer status, Long id) {
        log.info("菜品起售和停售：status:{},id:{}", status, id);
        dishService.startOrStop(status, id);

        // 将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * @param id
     * @auther: Yusc
     * @date 2023/10/28 16:25
     * @Description 根据id查询菜品
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询菜品")
    public Result<DishVO> getById(@PathVariable Long id) {
        log.info("根据id查询菜品：{}", id);
        DishVO dishVO = dishService.getByIdWithFlavor(id);
        return Result.success(dishVO);
    }

    /**
     * @param dishDTO
     * @return
     * @auther: Yusc
     * @date 2023/10/28 16:47
     * @Description 修改菜品
     */
    @PutMapping
    @ApiOperation("修改菜品")
    public Result update(@RequestBody DishDTO dishDTO) {
        log.info("修改菜品：{}", dishDTO);
        dishService.updateWithFlavor(dishDTO);

        // 将所有的菜品缓存数据清理掉，所有以dish_开头的key
        cleanCache("dish_*");
        return Result.success();
    }

    /**
     * @param categoryId
     * @return
     * @auther: Yusc
     * @date 2023/10/28 17:13
     * @Description 根据分类id查询菜品
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId) {
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * @param pattern redis的key
     * @auther: Yusc
     * @date 2023/10/31 20:51
     * @Description 清理缓存数据
     */
    private void cleanCache(String pattern) {
        Set keys = redisTemplate.keys(pattern);
        redisTemplate.delete(keys);
    }
}
